package com.huobao.shangjia_version.activitys;

import java.util.List;
import java.util.Map;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.adapters.Adapter_Progect_Manager;
import com.huobao.yunying.entity.ProgectManager;
import com.huobao.yunying.fragments.BaseFragment;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.zhangying.R;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.TextView;

public class Store_ProgectManagerFragment extends BaseFragment implements OnScrollListener,OnCheckedChangeListener {

	private boolean isLoading = true;
	private ListView listview;
	private String type;
	private int m_CurrentPage = 1;
	private TextView tv_foot_msg;
	private CheckBox checkBox;
	private TextView btn_binaji;
	private Adapter_Progect_Manager adapter;

	public static Store_ProgectManagerFragment getInstant(String type){
		Store_ProgectManagerFragment instance = new Store_ProgectManagerFragment();
		Bundle args = new Bundle();
		args.putString("type",type);
		instance.setArguments(args);
		return instance;
	}
	@Override
	public View onCreateContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		EventBus.getDefault().register(this);
		View view = inflater.inflate(R.layout.test, null);
		this.type = getArguments().getString("type");
		listview = (ListView) view.findViewById(R.id.listview);
		listview.setOnScrollListener(this);
		View FootView = inflater.inflate(R.layout.item_footview, null);
		tv_foot_msg = (TextView) FootView.findViewById(R.id.tv_message);
		listview.addFooterView(FootView);
		View headView = inflater.inflate(R.layout.item_progect_headview, null);
		checkBox = (CheckBox) headView.findViewById(R.id.checkbox);
		checkBox.setOnCheckedChangeListener(this);
		btn_binaji = (TextView) headView.findViewById(R.id.btn_1);
		listview.addHeaderView(headView);
		adapter = new Adapter_Progect_Manager(getActivity());
		listview.setAdapter(adapter);
		btn_binaji.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				StringBuffer stringBuffer = new StringBuffer();
				for(ProgectManager manager : adapter.getList()){
					if(manager.isChecked){
						stringBuffer.append(manager.goods_id);
						stringBuffer.append(",");
					}
				}
				Intent intent = new Intent(getActivity(), Act_Progect_Edit.class);
				intent.putExtra("ids", stringBuffer.toString().substring(0, stringBuffer.toString().length()-1));
				getActivity().startActivityForResult(intent, 1);
			}
		});
		getDate(true);
		return view;
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}
	
	@Subscriber(tag = "refresh")
	public void refresh(String s){
		isLoading = false;
		m_CurrentPage = 1;
		getDate(true);
	}

	private void getDate(boolean isShowDialog) {
		isLoading = true;
		Map<String, String> map = HttpUtil.getParams("app", "mygoods", "show", type, "page", m_CurrentPage + "");
		Http.getInstant().sendRequest(getActivity(), isShowDialog, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				List<ProgectManager> list = JSON.parseArray(jsonObject.getString("retval"), ProgectManager.class);
				if (list == null || list.size() == 0) {
					tv_foot_msg.setText("加载完成");
					isLoading = true;
					return;
				}
				adapter.addAll(list);
				isLoading = false;
				showContentView();
			}

			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub

			}
		});
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		if (isLoading)
			return;
		if ((firstVisibleItem + visibleItemCount) == totalItemCount) {
			m_CurrentPage++;
			getDate(false);
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		for(ProgectManager manager : adapter.getList()){
			manager.isChecked = isChecked;
		}
		adapter.notifyDataSetChanged();
	}
}
